package cn.gwm.flink.streaming.strategy.cloudpredict;

import cn.gwm.flink.streaming.constant.ChargePredictFields;
import cn.gwm.flink.streaming.strategy.vehiclepredict.DwsStrConstant;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * @author : GW00280745
 * @date : 2023/1/9 14:05
 * @description :
 * 维度的排列
 *  地点
 *  电流
 *  温度
 *  soc
 *  bms_heatreq
 *  acopensts
 *
 */
public class CloudMatchDimension implements Serializable {

    private String vidStr = " substr(id,0,17) =\'";
    private String vidStrNe = " substr(id,0,17) !=\'";
    private String quotationStr ="\' ";

    public StringBuilder deviceIdDim(StringBuilder stringBuilder,Object vin,Boolean isEqual){
        if (isEqual){
            stringBuilder.append(vidStr).append(vin).append(quotationStr);
        }else {
            stringBuilder.append(vidStrNe).append(vin).append(quotationStr);
        }
        return stringBuilder;
    }
    private String andStr = " and ";
    public StringBuilder andOps(StringBuilder stringBuilder){
        stringBuilder.append(andStr);
        return stringBuilder;
    }

    private String steadyTime80 = "  length(\"steady_time_80\")>0 ";
    private String steadyTime10 = " and length(\"steady_time_10\")>0 ";
    public StringBuilder steadyTimeDim(StringBuilder stringBuilder){
        stringBuilder.append(steadyTime80);
        stringBuilder.append(steadyTime10);
        return stringBuilder;
    }

    private String obcF = "  \"obc_connectsts_ev\"=\'";
    private String dcF = " and \"bms_dcchrgconnect\"=\'";
    public StringBuilder dcObcDim(StringBuilder stringBuilder,Object obc,Object dc){
        stringBuilder.append(obcF).append(obc).append(quotationStr);
        stringBuilder.append(dcF).append(dc).append(quotationStr);
        return stringBuilder;
    }

    public StringBuilder poiDim(StringBuilder stringBuilder,Object poi,Object aoi){
        //这个条件可能有也可能没有,有如下四种情况
        //1 poi=-9  aoi = -9 则没有这个条件
        //2 poi=-9  aoi = A  则条件是 aoi=A
        //3 poi=A   aoi = -9 则条件是 poi=A
        //4 poi=A   aoi = A 则条件是 poi=A || aoi=A
        boolean poi1 = poi == null;
        boolean poi2 = poi != null && StringUtils.equalsIgnoreCase(poi.toString(), ChargePredictFields.ins().defaultVal);

        boolean aoi1 = aoi == null;
        boolean aoi2 = aoi != null && StringUtils.equalsIgnoreCase(aoi.toString(), ChargePredictFields.ins().defaultVal);

        if (poi1 || poi2){
            // poi = -9 或者 空
            if (aoi1 || aoi2){
                //没有这个条件
                return stringBuilder;
            }else {
                //条件是 aoi =A
                stringBuilder.append(" and \"aoiid\" =\'").append(aoi).append(quotationStr);
            }
        }else {
            // poi = A
            if (aoi1 || aoi2){
                //条件是 poi = A
                stringBuilder.append(" and \"poiid\" =\'").append(poi).append(quotationStr);
            }else {
                //条件是 poi=A || aoi=A
                stringBuilder.append(" and ( \"poiid\" =\'").append(poi).append(quotationStr)
                        .append(" or \"aoiid\"=\'").append(aoi).append(quotationStr).append(" )");
            }
        }
        return stringBuilder;
    }

    private String startTimeStr = " \"startchargetime\"<\'";
    private String startTimeStr1 = " \"startchargetime\"<\'";
    public StringBuilder startTimeDim(StringBuilder stringBuilder,Object tidVal,boolean firstTime){
        //查找最近60天内的充电记录
        if (firstTime){
            return stringBuilder.append(startTimeStr1).append(tidVal).append(quotationStr);
        }
        stringBuilder.append(startTimeStr).append(tidVal).append(quotationStr);
        return stringBuilder;
    }

    private String rnDimStr = " \"rn\" <= \'0005\' and \"rn\" >= \'0001\' ";
    public StringBuilder rnDimStr(StringBuilder stringBuilder, Object tidVal){
        stringBuilder.append(rnDimStr);
        return stringBuilder;
    }
    private String modelCodeDimStr = " \"model_code\"=\'";
    public StringBuilder modelDim(StringBuilder stringBuilder,Object tidVal){
        stringBuilder.append(modelCodeDimStr).append(tidVal).append(quotationStr);
        return stringBuilder;
    }
    private String socStrGe1 = "  \"bms_innersoc\">=";
    private String socStrLe1 = " and \"bms_innersoc\"<=";
    public StringBuilder socDim(StringBuilder stringBuilder,Object bmsSoc){
        //soc当作字符串比较大小因此需要进行补位 以100.0为基准
        float soc = Math.abs(Float.parseFloat(bmsSoc.toString()));
        float v = soc - 5;
        if (v<0){
            v=0.0f;
        }
        stringBuilder.append(socStrGe1).append(quotationStr1).append(socConcat(v)).append(quotationStr);
        stringBuilder.append(socStrLe1).append(quotationStr1).append(socConcat(soc+5)).append(quotationStr);
        return stringBuilder;
    }
    private String startSocStrGe1 = "  \"startsoc\">=";
    private String startSocStrLe1 = " and \"startsoc\"<=";
    public StringBuilder startSocDim(StringBuilder stringBuilder,Object bmsSoc){
        //soc当作字符串比较大小因此需要进行补位 以100.0为基准
        float soc = Math.abs(Float.parseFloat(bmsSoc.toString()));
        float v = soc - 5;
        if (v<0){
            v=0.0f;
        }
        stringBuilder.append(startSocStrGe1).append(quotationStr1).append(socConcat(v)).append(quotationStr);
        stringBuilder.append(startSocStrLe1).append(quotationStr1).append(socConcat(soc+5)).append(quotationStr);
        return stringBuilder;
    }
    private String socConcat(float socVal){
        // 先进行位数
        socVal = new BigDecimal(socVal).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        //统一补齐到5位
        String socValStr = String.valueOf(socVal);
        int length = socValStr.length();
        if (length==1){
            return "0000".concat(socValStr);
        }else if (length== DwsStrConstant.ins().num2){
            return "000".concat(socValStr);
        }else if (length==DwsStrConstant.ins().num3){
            return "00".concat(socValStr);
        }else if (length==DwsStrConstant.ins().num4){
            return "0".concat(socValStr);
        }
        return socValStr;
    }
    private String currStrObcLt = "  \"bms_battcurr_rm\" <=\'";
    private String currStrObcGt = "  \"bms_battcurr_rm\" >=\'";
    public StringBuilder currDimObc(StringBuilder stringBuilder, Object bmsBattcurr,Object voltMax){
        //功率 = 电流 * 电压
        float currF = Float.parseFloat(bmsBattcurr.toString());
        float currAbs = Math.abs(currF);
        float voltXf = Float.parseFloat(voltMax.toString());
//        float v1 = currAbs * voltXf -2;
        float v1 = currAbs  -2;
        if (v1<DwsStrConstant.ins().num0){
            v1=0;
        }
//        float v = currAbs * voltXf + 2;
        float v = currAbs  + 2;
        if (v1<DwsStrConstant.ins().num10){
            stringBuilder.append(currStrObcGt).append("-0").append(v1).append(quotationStr);
        }else {
            stringBuilder.append(currStrObcGt).append("-").append(v1).append(quotationStr);
        }
        stringBuilder.append(andStr);
        if (v<DwsStrConstant.ins().num10){
            stringBuilder.append(currStrObcLt).append("-0").append(v).append(quotationStr);
        }else {
            stringBuilder.append(currStrObcLt).append("-").append(v).append(quotationStr);
        }
        return stringBuilder;
    }
    public StringBuilder currDimObc2(StringBuilder stringBuilder, Object bmsBattcurr,Object voltMax){
        //功率 = 电流 * 电压
        float currF = Float.parseFloat(bmsBattcurr.toString());
        float currAbs = Math.abs(currF);
        float voltXf = Float.parseFloat(voltMax.toString());
//        float v1 = currAbs * voltXf -2;
        float v1 = currAbs  -2;
        if (v1<DwsStrConstant.ins().num0){
            v1=0;
        }
//        float v = currAbs * voltXf + 2;
        if (v1<DwsStrConstant.ins().num10){
            stringBuilder.append(currStrObcGt).append("-00").append(v1).append(quotationStr);
        }else if (v1<DwsStrConstant.ins().num100){
            stringBuilder.append(currStrObcGt).append("-0").append(v1).append(quotationStr);
        }else if (v1<DwsStrConstant.ins().num200){
            stringBuilder.append(currStrObcGt).append("-").append(v1).append(quotationStr);
        }
        stringBuilder.append(andStr);
        float v = currAbs  + 2;
        if (v<DwsStrConstant.ins().num10){
            stringBuilder.append(currStrObcLt).append("-00").append(v).append(quotationStr);
        }else if (v<DwsStrConstant.ins().num100){
            stringBuilder.append(currStrObcLt).append("-0").append(v).append(quotationStr);
        }else if (v<DwsStrConstant.ins().num200){
            stringBuilder.append(currStrObcLt).append("-").append(v).append(quotationStr);
        }
        return stringBuilder;
    }
    private String acAmbLt = "  \"bms_rmc_moduletempmin\" <=";
    private String acAmbGt = "  \"bms_rmc_moduletempmin\" >=";
    private String quotationStr1 ="\'";
    public StringBuilder tempDim(StringBuilder stringBuilder,Object acambtemp){
        int tempVal = Integer.parseInt(acambtemp.toString());
        if (tempVal<0){
            //温度是负数 -2 -8
            int abs = Math.abs(tempVal);
            int i = abs+2;
            int j = abs-2;
            if (i>=DwsStrConstant.ins().num10){
                stringBuilder.append(acAmbLt).append(quotationStr1).append("-").append(i).append(quotationStr).append(andStr);
            }else {
                stringBuilder.append(acAmbLt).append(quotationStr1).append("-0").append(i).append(quotationStr).append(andStr);
            }

            if (j>=DwsStrConstant.ins().num10){
                stringBuilder.append(acAmbGt).append(quotationStr1).append("-").append(j).append(quotationStr);
            }else if(j>=0){
                stringBuilder.append(acAmbGt).append(quotationStr1).append("-0").append(j).append(quotationStr);
            }else {
                stringBuilder.append(acAmbGt).append(quotationStr1).append("-0").append(Math.abs(j)).append(quotationStr);
            }
        }else if (tempVal>=DwsStrConstant.ins().num0){
            int i = tempVal+2;
            if (i<DwsStrConstant.ins().num10){
                stringBuilder.append(acAmbLt).append(quotationStr1).append("a0").append(i).append(quotationStr).append(andStr);
            }else {
                stringBuilder.append(acAmbLt).append(quotationStr1).append("a").append(i).append(quotationStr).append(andStr);
            }
            int j = tempVal-2;
            if (j>=DwsStrConstant.ins().num10){
                stringBuilder.append(acAmbGt).append(quotationStr1).append("a").append(j).append(quotationStr);
            }else if (j>=0){
                stringBuilder.append(acAmbGt).append(quotationStr1).append("a0").append(j).append(quotationStr);
            }else {
                stringBuilder.append(acAmbGt).append(quotationStr1).append("-0").append(Math.abs(j)).append(quotationStr);
            }
        }
        return stringBuilder;
    }

    private String orderByStr = " order by ";
    private String orderSctObc =" \"poiDim\" desc, \"deltaSocAbs\" asc, \"deltaSoc\" asc,\"startchargetime\" desc limit 1";


    public StringBuilder orderByObc(StringBuilder stringBuilder){
        stringBuilder.append(orderByStr);
        stringBuilder.append(orderSctObc);
        return stringBuilder;
    }

    public StringBuilder orderByObcDefault(StringBuilder stringBuilder){
        stringBuilder.append(orderByStr);
        stringBuilder.append(orderSctObc);
        return stringBuilder;
    }
}
